﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;

namespace DBA.App.Dictionary
{
    // 
    public  class GoogleTranslater
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        public static bool IsConnectedToInternet()
        {

            int Desc;
            return InternetGetConnectedState(out Desc, 0);

        }
        public static bool IsServerActive()
        {
            Ping p = new Ping();
            PingReply reply = p.Send("74.125.71.113", 500);
            if (reply.Status.ToString() == "Success")
                return true;
            return false;
        }

        public static String Translate(String strText, String strFromLanguage, String strToLanguage)
        {

            // make sure that the passed string is not empty or null

            if (!String.IsNullOrEmpty(strText))
            {                          

                // a string of up to 5000 characters long

                if (strText.Length <= 5000)
                {                
                    try
                    {

                       
                        string requestUri = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" +

                            strText + "&langpair=" +

                             strFromLanguage + "%7C" + strToLanguage;

                        // execute the request and get the response stream
                        string result;

                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                        Stream responseStream = response.GetResponseStream();
                       
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            result = reader.ReadToEnd();
                        }
                        // the format of the response is like this
                        // {"responseData": {"translatedText":"this is a dog"}, "responseDetails": null, "responseStatus": 200}

                        result=result.Remove(0, 36);

                        result=result.Substring(0, result.IndexOf("\""));

                        return result;
                       
                    }

                    catch
                    {

                        return "Cannot get the translation.  Please try again later.";

                    }

                }

                else
                {

                    return "String to translate must be less than 5000 characters long.";

                }

            }

            else
            {
                return "String to translate is empty.";

            }

        }
    }
}
